package club.xinghe.servlet.admin;

import club.xinghe.biz.AdminBiz;
import club.xinghe.entity.*;
import club.xinghe.util.JsonServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @create刘艺广
 */
@WebServlet("/admin/ScoreFenXiServlet")
public class ScoreFenXiServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int gid=Integer.parseInt(request.getParameter("gId"));   //年级id
        int yId=Integer.parseInt(request.getParameter("yId"));  //院系id
        int zId=Integer.parseInt(request.getParameter("zId"));  //专业id

        AdminBiz adminBiz=new AdminBiz();
        Student student;
        Yx yx;
        Zy zy;
        Score score;
        List<Zy> zyList=null;
        List<Yx> yxList=null;
        List<Score> scoreList=null;
        if(gid!=0){                         //根据年级查询
            student=new Student(gid);       //学生
            score=new Score(student);         //成绩
            yxList=adminBiz.getAllYx();     //得到所有院系，前台添加到下拉列表
            scoreList=adminBiz.getScoreByGid(score);    //得到年级的成绩列表
            if(yId!=0){                            //根据年级和院系查询
                yx=new Yx(yId);
                student=new Student(yx,gid);
                zyList=adminBiz.getZyByYid(yId);    //根据院系id查询的所有专业
                score=new Score(student);
                scoreList=adminBiz.getScoreByYid(score);//得到院系的成绩列表
                if(zId!=0){                         //根据年级，院系和专业查询
                    zy=new Zy(zId);
                    student=new Student(zy,yx,gid);
                    score=new Score(student);
                    scoreList=adminBiz.getScoreByZid(score);//得到专业的成绩列表
                }
            }
        }

        Map<String,Object> map=new HashMap<>();
        if(zyList!=null){
            map.put("zyList",zyList);
        }
        if(scoreList!=null){
            map.put("scoreList",scoreList);
        }
        if(yxList!=null){
            map.put("yxList",yxList);
        }
        if(map.size()>0){
            JsonServlet.getJson(request,response,map);
        }else {
            PrintWriter out=response.getWriter();
            out.print("null");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
